<template>
  <span>{{ type == "self" ? timeLeft : initialTime }}</span>
</template>

<script>
export default {
  props: {
    // 初始倒计时时间，单位秒
    initialTime: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      timeLeft: this.initialTime,
      timer: null,
      type: "",
    };
  },
  created() {
    // this.startCountdown();
  },
  beforeDestroy() {
    this.clearTimer();
  },
  methods: {
    startCountdown() {
      this.type = "self";
      this.timeLeft = this.initialTime;
      this.timer = setInterval(() => {
        this.$emit("ok", this.timeLeft);
        if (this.timeLeft > 0) {
          this.timeLeft--;
        } else {
          this.clearTimer();
        }
      }, 1000);
    },
    settype() {
      this.clearTimer();
      this.type = "";
    },
    clearTimer() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
  },
};
</script>
